package com.qfdqc.views.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

import com.google.gson.reflect.TypeToken;
import com.qfdqc.views.seattable.JSOn;
import com.qfdqc.views.seattable.People;
import com.qfdqc.views.seattable.SeatTable;
import com.qfdqc.views.seattable.TxtUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    public SeatTable seatTableView;
    List<JSOn.ColumnsFieldBean> columnsField;
    List<JSOn.RowsFieldBean> rowsField;
    Map<String, String> lie = new HashMap();
    ArrayList<String> ccList = new ArrayList<String>();
    int rowStartNumberField;
    List<People> peoplelist = new ArrayList<People>();
    String name, zuowei;
    BanSui_Adapter banSui_adapter;
    TxtUtils txtUtils = new TxtUtils();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        banSui_adapter = new BanSui_Adapter();
        Spinner spinner = (Spinner) findViewById(R.id.sp);
        spinner.setAdapter(banSui_adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                People people = peoplelist.get(position);
                name = people.getName();
                zuowei = people.getZuowei();
                txtUtils.setPeople(people);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        String s = TxtUtils.readAssetsTxt(MainActivity.this, "vv");
        peoplelist.add(new People("張三", ""));
        peoplelist.add(new People("李四", ""));
        name = peoplelist.get(0).getName();

        banSui_adapter.addItem(peoplelist);
        Type type = new TypeToken<ArrayList<JSOn>>() {
        }.getType();

        ArrayList<JSOn> jsOn = JSONUtils.parseJSONArray(s, type);
        //多少列
        JSOn jsOn1 = jsOn.get(0);
        columnsField = jsOn1.getColumnsField();
        rowsField = jsOn1.getRowsField();
        rowStartNumberField = jsOn1.getRowStartNumberField();
        int rowEndNumberField = jsOn1.getRowEndNumberField();
        for (int i = rowStartNumberField; i < rowEndNumberField; i++) {
            ccList.add("" + i);
        }


        Log.e("zzz", "size" + columnsField.size());

        SeatTable.SeatChecker seatChecker = new SeatTable.SeatChecker() {

            @Override
            public boolean isValidSeat(int row, int column) {
                lie.clear();
                if (columnsField.get(column).getDescriptionField() == 2) {
                    lie.put("" + column, columnsField.get(column).getCodeField());
                    return false;
                } else {
                    lie.put("" + column, columnsField.get(column).getCodeField());
                    return true;
                }


            }

            @Override
            public boolean isSold(int row, int column) {
                //行 row
                // 列column  a c d
                //第几行的座位
                JSOn.RowsFieldBean rowsFieldBean = rowsField.get(row);
                //第几行的所有座位
                List<JSOn.RowsFieldBean.SeatsFieldBean> seatsField = rowsFieldBean.getSeatsField();
                //第0个的第A座
//               String codeField = seatsField.get(column).getCodeField();
                String s1 = lie.get("" + column);
                //ACD 只显示正常的座位

                for (int i = 0; i < seatsField.size(); i++) {
                    if (seatsField.get(i).getCodeField().equals(s1)) {
                        if (seatsField.get(i).isIsAvailField()) {
                            return false;
                        }
                    }
                }
                return true;


            }

            @Override
            public void checked(int row, int column) {
                Log.e("1112", "row" + row + "column" + column);
                String s1 = lie.get("" + column);
                Log.e("1112", "row" + (rowStartNumberField + row) + "column" + s1);
                for (int i = 0; i < peoplelist.size(); i++) {
                    if (peoplelist.get(i).getName().equals(name)) {
                        peoplelist.get(i).setZuowei(rowStartNumberField + row + "/" + s1);
                        txtUtils.setPeople(peoplelist.get(i));
                        banSui_adapter.notifyDataSetChanged();
                    }

                }

            }

            @Override
            public void unCheck(int row, int column) {
                Log.e("112", "row" + row + "column" + column);
                for (int i = 0; i < peoplelist.size(); i++) {
                    if (peoplelist.get(i).getName().equals(name)) {
                        peoplelist.get(i).setZuowei("");
                        txtUtils.setPeople(peoplelist.get(i));
                        banSui_adapter.notifyDataSetChanged();
                    }

                }
            }

            @Override
            public String[] checkedSeatTxt(int row, int column) {
                return null;
            }

        };

        seatTableView = (SeatTable)

                findViewById(R.id.seatView);
        seatTableView.setScreenName("xxx");//设置屏幕名称
        seatTableView.setMaxSelected(peoplelist.size());//设置最多选中
        seatTableView.setLineNumbers(ccList);
        seatTableView.setSeatChecker(seatChecker);
        seatTableView.setData(rowsField.size(), columnsField.size(), rowStartNumberField - 1, columnsField,txtUtils);


    }

}
